package test

import (
	"log"

	"time"

	"github.com/gogf/gf/util/gconv"
	"idoone.cc/idoonego/db/redis"
)

func TestRedis() {
	ConfigSetUp()
	redis.SetConfig(GlobalSetting.RedisConfig)

	TestBasic()
	TestZset()

	for i := 0; i < 100; i++ {
		go redisMoveToNew()
		go redisMoveFromNew()
	}
}

func redisMoveToNew() {
	for {
		queue := "x0kqmaxx5701l07wh05047_syncMsg"
		apply, err := redis.RPOP(queue)
		if apply != nil && err == nil {
			redis.LPUSH(queue+"_New", apply)
			//log.Println(string(apply.([]byte)))
		} else if err != nil {
			log.Println("apply:", apply, "err:", err)
		}
		time.Sleep(time.Second)
	}
}

func redisMoveFromNew() {
	for {
		queue := "x0kqmaxx5701l07wh05047_syncMsg"
		apply, err := redis.LPOP(queue + "_New")
		if apply != nil && err == nil {
			redis.RPUSH(queue, apply)
			//log.Println(string(apply.([]byte)))
		} else if err != nil {
			log.Println("apply:", apply, "err:", err)
		}
		time.Sleep(time.Second)
	}
}

func TestBasic() {
	err := redis.DEL("a")
	log.Println("DEL", err)
	a, err := redis.GET("a")
	log.Println("GET", a, err)
	err = redis.SET("a", "a")
	log.Println("SET", err)
	hasKey, err := redis.EXISTS("a")
	log.Println("EXISTS", hasKey, err)
	b, err := redis.GET("a")
	log.Println("GET", gconv.String(b), err)
	redis.SETEX("b", "ex", 60)
	ttl, err := redis.TTL("b")
	log.Println("TTL", ttl)
}

func TestZset() {
	ret, err := redis.ZADD("zadd", 1.1, "a")
	log.Println(ret, " ", err)
	ret, err = redis.ZADD("zadd", 1.1, "b")
	log.Println(ret, " ", err)
	ret, err = redis.ZINCRBY("zadd", 1.1, "a")
	log.Println(gconv.String(ret), " ", err)
}
